<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Common\Model;

class SettingModel extends CommonModel
{
    public function fetchAll() {

        if (!$data = F($this->trueTableName)) {
            $result = $this->select();
            foreach($result as $row) {
                $data[$row['key']] = json_decode($row['value'], true);
            }
            F($this->trueTableName, $data);
        }
        return $data;
    }

    public function save($arr) {
        if($this->find(array("where" => array('key' => $arr['key'])))){
            return parent::save($arr);
        }else{
            return $this->add($arr);
        }
    }

    public function getSettingByKey($key) {
        $settings = $this->fetchAll();
        if (isset($settings[$key])) {
            return $settings[$key];
        }
        return array();
    }
}

?>